﻿using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.CacheService;
using Fuse8.DomainFramework.Domain;
using System;
using System.Web.Script.Serialization;
using System.Xml.Serialization;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsPage : CmsRevisionableEntityObject
    {
        #region Linked fields

        private CmsPage _parentPage;

        private CmsTemplate _template;

        #endregion Linked fields

        #region Properties

        /// <summary>
        /// Public page id
        /// </summary>
        public virtual int PublicPageId { get; set; }

        /// <summary>
        /// Parend page id
        /// </summary>
        public virtual int? ParentPageId { get; set; }

        /// <summary>
        /// Page's template
        /// </summary>
        public virtual int TemplateId { get; set; }

        /// <summary>
        /// Page type key
        /// </summary>
        public virtual int PageTypeKey { get; set; }

        /// <summary>
        /// Page type
        /// </summary>
        public virtual int PageBehaviourTypeKey { get; set; }

        /// <summary>
        /// Page level - 1 for root page
        /// </summary>
        public virtual int PageLevel { get; set; }

        /// <summary>
        /// Name viewed in administration
        /// </summary>
        public virtual string PageName { get; set; }

        /// <summary>
        /// Page's URL
        /// </summary>
        public virtual string PageUrl { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual bool IsVisible { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual string PageTitle { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual string PageKeywords { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual string PageDescription { get; set; }

        /// <summary>
        ///
        /// </summary>
        public bool UseFileCache { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual string PageModelType { get; set; }

        /// <summary>
        ///
        /// </summary>
        public virtual string PageKey { get; set; }

        #endregion Properties

        #region Linked properties

        [XmlIgnore]
        [ScriptIgnore]
        public CmsTemplate Template
        {
            get
            {
                if (_template == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsTemplate, int>();
                    if (businessFacade != null)
                    {
                        _template = businessFacade.FindOne(TemplateId);
                    }
                }
                return _template;
            }
            set
            {
                TemplateId = value != null ? value.Id : 0;

                _template = value;
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public CmsPage ParentPage
        {
            get
            {
                if (_parentPage == null)
                {
                    if (ParentPageId.HasValue && ParentPageId != 0)
                    {
                        var businessFacade = new BusinessServiceFacade<CmsPage, int>();
                        _parentPage = businessFacade.FindOne(ParentPageId.Value);
                    }
                }

                return _parentPage;
            }
            set
            {
                ParentPageId = value != null ? value.Id : new Nullable<int>();

                _parentPage = value;
            }
        }

        #endregion Linked properties

        #region Constructor

        public CmsPage()
        { }

        #endregion Constructor

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "Page";
            }
        }

        #endregion EntityObject override
    }
}